package vip.xiaonuo.facedoor.modular.reactor.listener;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.Reactor;
import reactor.event.Event;
import reactor.function.Consumer;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.facedoor.modular.memberuser.entity.MemberUser;
import vip.xiaonuo.facedoor.modular.memberuser.service.MemberUserService;
import vip.xiaonuo.facedoor.modular.orgdevice.entity.OrgDevice;
import vip.xiaonuo.facedoor.modular.orgdevice.mapper.OrgDeviceMapper;
import vip.xiaonuo.facedoor.modular.topic.EventTopic;
import vip.xiaonuo.facedoor.modular.visitor.entity.Visitor;
import vip.xiaonuo.facedoor.modular.visitor.service.VisitorService;
import vip.xiaonuo.facedoor.modular.visitordevice.entity.VisitorDevice;
import vip.xiaonuo.facedoor.modular.visitordevice.mapper.VisitorDeviceMapper;
import vip.xiaonuo.sys.modular.org.entity.SysOrg;
import vip.xiaonuo.sys.modular.org.service.SysOrgService;

import javax.annotation.Resource;
import java.util.List;

import static reactor.event.selector.Selectors.$;

/**
 * 访客机新增访客 监听 门禁设备增加
 */
@Service
@Slf4j
public class FacedoorVisitorDeviceAddListener implements Consumer<Event<Long>>, InitializingBean {


    @Autowired
    private Reactor reactor;

    @Autowired
    private VisitorService visitorService;

    @Resource
    private VisitorDeviceMapper visitorDeviceMapper;

    @Resource
    private OrgDeviceMapper orgDeviceMapper;

    @Autowired
    private SysOrgService sysOrgService;

    @Autowired
    private MemberUserService memberService;

    @Override
    public void accept(Event<Long> event) {
        Visitor visitor = visitorService.getById(event.getData());
        if (visitor == null) {
            throw new ServiceException(500, "访客不存在");
        }
        log.info("小程序新增访客 监听 梯控设备增加【{}】", visitor.getName());

        Long orgId;
        SysOrg sysOrg = sysOrgService.getById(visitor.getCompanyId());
        if (sysOrg == null) {
            throw new ServiceException(500, "找不到被访人公司");
        }
        // 默认使用公司的组织
        orgId = sysOrg.getId();

        List<VisitorDevice> visitorDevices = visitorDeviceMapper.selectList(new LambdaQueryWrapper<VisitorDevice>().eq(VisitorDevice::getVisitorId, visitor.getId()));
        if (CollectionUtil.isNotEmpty(visitorDevices)) {
            log.error("访客【{}】已经下发到设备中 不进行二次下发", visitor.getName());
            return;
        }

        // 如果邀请人不为null  则使用邀请人的组织
        MemberUser invitorMember = memberService.getOne(new LambdaQueryWrapper<MemberUser>().eq(MemberUser::getTel, visitor.getInviterPhone()));
        if (invitorMember != null) {
            orgId = invitorMember.getOrgId();
        }
        // 权限复制
        List<OrgDevice> orgDevices = orgDeviceMapper.selectList(new LambdaQueryWrapper<OrgDevice>().eq(OrgDevice::getOrgId, orgId));
        for (OrgDevice orgDevice : orgDevices) {
            VisitorDevice memberDevice = new VisitorDevice()
                    .setStatus(1)
                    .setOrgId(orgId)
                    .setDeviceId(orgDevice.getDeviceId())
                    .setVisitorId(visitor.getId());
            visitorDeviceMapper.insert(memberDevice);
        }

    }

    @Override
    public void afterPropertiesSet() throws Exception {
        reactor.on($(EventTopic.VISITOR_DEVICE_ADD), this);
    }
}

